<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/27
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;
use fast\Http;

/**
 * 文字转语音🔊
 * 
 */
class Tts extends Tpmecms
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];




    /**
     * 获取tts到文件路径包含生成下载
     *
     * @param string $str 文字内容
     * @return void
     */
    protected function GetTtsPath($str)
    {

        $url = Config::get('site.tts_url');
        
        if(!$url){
            $this->error('请配置文字转语音接口地址');
        }
        // $url_path = file_get_contents($url.'?str='.$str);
        // $url_path = Http::get($url.'/tts'.'?str='.$str);
        $url_path = Http::get($url.'?str='.$str);
        // echo($url_path);
        // exit();
        $url_path = trim($url_path);
    
        // $url_path = $this->Posttts($str);

        if($url_path!='strno' || !empty($url_path)){
           
            
            $download_url = $url . $url_path;   //下载路径

            // halt($download_url);
            $save_path = $url_path;         //保存本地路径
            file_put_contents('.'.$save_path,Http::get($download_url));
            return $save_path;
        }
    }

    /**
     * 获取文字转语音🔊
     *
     * @param string $str 文字内容
     */
    public function get_tts($str)
    {
        if(!$str){
            $this->error('没有内容');
        }

        $md5 = md5($str);
        $where['md5'] = $md5;
        $where['str'] = $str;
        //如果有就直接返回
        $res = Db::name('tts')->where($where)->find();
        if($res){
            $this->AddTtsNum($res['id']);
            $this->success('ok',$res);
        }

        $data['path'] = $this->GetTtsPath($str);//获取tts文件路径 需要保存到本服务器上
        $data['md5'] = $md5;
        $data['str'] = $str;
        $data['time'] = time();
        $data['num'] = 1;

        Db::name('tts')->insert($data);
        $this->success('ok',$data);

    }


    /**
     * 文字转语音下载地址🔊
     *
     * @param string $str 文字内容
     */
    public function StrChangeSpeech($str)
    {
        $url = Config::get('site.tts_url');
        
        if(!$url){
            $this->error('请配置文字转语音接口地址');
        }
        $url_path = file_get_contents($url.'/tts?str='.$str);
        // $url_path = Http::Post($url.'/tts',['str'=>$str]);
        // print_r($url_path);
        $url_path = trim($url_path);
        $this->success('ok',$url.$url_path);
    }



    /**
     * 文字转语音下载地址🔊
     *
     * @param string $str 文字内容
     */
    protected function Posttts($str)
    {
        $url = Config::get('site.tts_url');
        
        if(!$url){
            $this->error('请配置文字转语音接口地址');
        }
        // $url_path = file_get_contents($url.'/tts?str='.$str);
        $url_path = Http::Post($url.'/tts',['str'=>$str]);
        // $url_path = trim($url_path);
        $data = json_decode($url_path,true);
        // halt($url_path);
        if($data)
            return $data['path'];
        $this->error('解析失败',$url_path);
        // $this->success('ok',$url_path);
    }

    /**
     * 增加使用次数
     *
     * @param integer $id ttsid
     * @param integer $num 使用次数默认1
     * @return void
     */
    protected function AddTtsNum($id,$num=1)
    {
        Db::name('tts')->where('id',$id)->setInc('num',$num);
    }
}